package com.applcn.core.base;


import com.applcn.core.enums.PlatformCodeEnum;

import java.util.HashMap;

/**
 * 统一返回结果构造工具
 * TODO 使用泛型改进
 */
public class Result extends HashMap<String, Object> {

    private static final long serialVersionUID = -3750377595494282725L;

    private static Result result;

    public Result() {
    }

    public static Result ok() {
        return put(PlatformCodeEnum.OK);
    }

    private Result(int initialCapacity) {
        super(initialCapacity);
    }

    public static Result ok(Object data) {
        return put(PlatformCodeEnum.OK, data);
    }

    public static Result ok(String msg) {
        return put(PlatformCodeEnum.OK, msg);
    }

    public static Result ok(Object data, String msg) {
        return put(PlatformCodeEnum.OK, data, msg);
    }

    public static Result error() {
        return put(PlatformCodeEnum.ERROR);
    }

    public static Result error(Object data) {
        return put(PlatformCodeEnum.ERROR, data);
    }

    public static Result error(String msg) {
        return put(PlatformCodeEnum.ERROR, msg);
    }

    public static Result error(Object data, String msg) {
        return put(PlatformCodeEnum.ERROR, data, msg);
    }

    public static Result error(String code, String msg) {
        return put(code, msg);
    }

    public static Result error(PlatformCodeEnum platformCodeEnum) {
        return put(platformCodeEnum);
    }

    public static Result put(PlatformCodeEnum platformCodeEnum) {
        result = new Result(4);
        result.putData("code", platformCodeEnum.getCode());
        result.putData("msg", platformCodeEnum.getMsg());
        return result;
    }

    public static Result put(String code, String msg) {
        result = new Result(5);
        result.putData("code", code);
        result.putData("msg", msg);
        return result;
    }

    public static Result put(PlatformCodeEnum platformCodeEnum, String msg) {
        result = new Result(4);
        result.putData("code", platformCodeEnum.getCode());
        result.putData("msg", msg);
        return result;
    }

    public static Result put(PlatformCodeEnum platformCodeEnum, Object data) {
        result = new Result(5);
        result.putData("code", platformCodeEnum.getCode());
        result.putData("msg", platformCodeEnum.getMsg());
        result.putData("data", data);
        return result;
    }


    public static Result put(PlatformCodeEnum platformCodeEnum, Object data, String msg) {
        result = new Result(5);
        result.putData("code", platformCodeEnum.getCode());
        result.putData("msg", msg);
        result.putData("data", data);
        return result;
    }

    private Result putData(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Result putData(Object value) {
        super.put("data", value);
        return this;
    }

    public Object getData(){
        return this.get("data");
    }

    public boolean isSuccess() {
        return this.get("code").equals(PlatformCodeEnum.OK.getCode());
    }
}
